/********************************************************************
Name: Figure 4: Smaller Partisan Differences Among Sheriffs 
	Than Public (Left Panel)
Author: Dan Thompson
Date: August 2019
Notes:
********************************************************************/

* Set the working directory 
gl root = "$sanctuary_root"
gl root = "YOUR_WORKING_DIRECTORY"

* Bring in the CCES data
use "$root/data/cces immigration attitude analysis data.dta", clear
keep if year==2012 & in_sheriff==1

* Set up the output container
gen row = _n
gen group = ""
gen p = ""
gen question = ""
gen mean = .
gen se = .

* Estimate the average public answer to the questions by party
local i = 1
qui foreach y in "for_questioning" "for_more_patrol" {
	foreach p in "dem" "rep" {
		reg `y' if `p'==1 [aw=weight1]
		replace mean = _b[_cons] if _n==`i'
		replace se = _se[_cons] if _n==`i'
		replace group = "public" if _n==`i'
		replace p = "`p'" if _n==`i'
		replace question = "`y'" if _n==`i'
		local i = `i' + 1
	}
}

* Store the means from the public
keep row group p question mean se
drop if group==""
tempfile results
save `results'

* Estimate the average sheriff's answer to the questions by party
use "$root/data/sheriff survey - cleaned.dta", clear
gen row = _n
merge 1:1 row using `results'
foreach y in "spend" "inquire" {
	replace bin_`y' = bin_`y'/100
	foreach p in "dem" "rep" {
		reg bin_`y' if `p'==1
		replace mean = _b[_cons] if _n==`i'
		replace se = _se[_cons] if _n==`i'
		replace group = "sheriff" if _n==`i'
		replace p = "`p'" if _n==`i'
		replace question = "`y'" if _n==`i'
		local i = `i' + 1
	}
}

* Store the means from the sheriffs and public
keep group p question mean se
rename p party
drop if group==""

* Mark the top and bottom of the 95% confidence interval
gen upper = mean + se*1.96
gen lower = mean - se*1.96

* Make the question variable the same across the public and sheriff datasets
replace question = "border" if inlist(question, "for_more_patrol", "spend")
replace question = "inquire" if inlist(question, "for_questioning", "inquire")

* Set up a variable for the running axis of the plot
gen v = .
local i = 1
foreach q in "border" "inquire" {
	foreach g in "public" "sheriff" {
		foreach p in "rep" "dem" {
			replace v = `i' if question=="`q'" & group=="`g'" & party=="`p'"
			local i = `i' + 1.1
		}
		local i = `i' + .2
	}
}

* Build and save plot
twoway (bar mean v if party=="rep" & group=="public", fc(maroon) lw(none)) ///
	(bar mean v if party=="dem" & group=="public", fc(navy) lw(none)) ///
	(bar mean v if party=="rep" & group=="sheriff", fc(maroon) fi(50) lw(none)) ///
	(bar mean v if party=="dem" & group=="sheriff", fc(navy) fi(50) lw(none)) ///
	(rcap upper lower v if party=="rep", lc(maroon)) ///
	(rcap upper lower v if party=="dem", lc(navy)), ///
	ysc(r(0 1)) yti("Share in Agreement") ///
	xlabel(2.75 "More Patrols on Border" 7.55 "Ask About Citizenship", ///
		valuelabel notick) ///
	xti("") xsc(r(0 10.2)) ///
	legend(order(1 "Republican Public" 2 "Democratic Public" ///
		3 "Republican Sheriffs" 4 "Democratic Sheriffs")) ///
	bgcolor(white) graphregion(color(white)) ///
	name("sheriff_cces_means", replace)
